package com.shyroke.daydayzhuanweb.controller;

import com.shyroke.daydayzhuanapi.dto.*;
import com.shyroke.daydayzhuansvc.service.impl.ProjectPartService;
import com.shyroke.daydayzhuansvc.util.Page;
import com.shyroke.daydayzhuansvc.util.R;
import com.shyroke.daydayzhuansvc.util.TabelData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * 类名 ：管理员管理项目
 * 用法 ：
 * 创建人 ： shyroke
 * 时间：2018/12/28 12:38
 */
@Controller
@RequestMapping(value = "admin/project/part")
public class AdminProjectPartController {

    @Autowired
    private ProjectPartService projectPartService;

    @GetMapping("")
    public String list(){
        return "admin/project/part/list";
    }

    /**
     * 文章组合列表
     * @return
     */
    @RequestMapping(value = "query")
    @ResponseBody
    public TabelData<AdminProjectPartDto> query(Page page, @RequestParam(value = "typeId",required = false)Integer typeId,
                                                @RequestParam(value = "type",required = false)String type,
                                                @RequestParam(value = "excludeType",required = false)String excludeType) {
        return  projectPartService.query(page,typeId,type,excludeType);

    }

    @GetMapping("addView")
    public String addView(){
        return "admin/project/part/add";
    }

    @GetMapping("editView")
    public ModelAndView editView(@RequestParam Integer id,ModelAndView modelAndView){
        modelAndView.setViewName("admin/project/part/edit");
        if (id != null) {
            AdminProjectPartDto dto = projectPartService.findById(id);
            if(dto != null) {
                modelAndView.addObject("dto",dto);
            }
        }
        return modelAndView;
    }

    @PostMapping("")
    @ResponseBody
    public R save(@RequestBody AdminProjectPartSaveDto dto){
        return projectPartService.save(dto);
    }

    @PutMapping("")
    @ResponseBody
    public R put(@RequestBody AdminProjectPartSaveDto dto){
        return projectPartService.save(dto);
    }

    @DeleteMapping("{id}")
    @ResponseBody
    public R delete(@PathVariable("id")Integer id){
        return projectPartService.del(id);
    }

    @GetMapping("createView")
    public ModelAndView createView(@RequestParam(required = false) String title, @RequestParam(required = false) Integer id, ModelAndView modelAndView){
        modelAndView.addObject("title",title);
        modelAndView.addObject("id",id);
        modelAndView.setViewName("admin/project/part/create");
        return modelAndView;
    }

    @PostMapping("createProject")
    @ResponseBody
    public R createProject(@RequestBody AdminProjectPartCreateDto dto, HttpSession session){
        return projectPartService.createProject(dto,session);
    }

}
